package com.codingke.codingbookstore.ui.fragment;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStrip;
import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.ui.SearchActivity;

import java.util.ArrayList;

/**
 * descreption:
 * company: codingke.com
 * Created by vince on 15/11/13.
 */
public class DiscoverFragment extends BaseFragment implements View.OnClickListener{

    private PagerSlidingTabStrip pagerSlidingTabStrip;
    private ViewPager viewPager;
    private LinearLayout ll_search_btn_container;
    private TextView tv_search_show;
    private LinearLayout load_layout;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private MyPagerAdapter myPagerAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover,container,false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        tv_search_show = (TextView) view.findViewById(R.id.tv_search_show);
        tv_search_show.setOnClickListener(this);
        pagerSlidingTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        viewPager = (ViewPager) view.findViewById(R.id.pager);

        fragments.add(new JavaCategoryFragment());
        fragments.add(new CCategoryFragment());
        fragments.add(new AndroidCategoryFragment());
        fragments.add(new IOSCategoryFragment());
        myPagerAdapter = new MyPagerAdapter(getFragmentManager());
        viewPager.setAdapter(myPagerAdapter);

//        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
//                .getDisplayMetrics());
//        viewPager.setPageMargin(pageMargin);
        pagerSlidingTabStrip.setViewPager(viewPager);
//
//        pagerSlidingTabStrip.setIndicatorColor(0xfff6f6f6);//指示器
        pagerSlidingTabStrip.setTextColor(0xFFF4F6F6);//文本
//        pagerSlidingTabStrip.setBackgroundColor(0xff489cfa);//背景
        pagerSlidingTabStrip.setTypeface(Typeface.DEFAULT, 0);//字体类型


        viewPager.setCurrentItem(0);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        startActivity(intent);
    }


    class MyPagerAdapter extends FragmentPagerAdapter{

        private String[] titles = {"Java","C/OC/C++","Android","iOS"};
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }

}
